package com.mike.fragmentdemo3.Fragment.Fragment;

import static android.content.Context.MODE_PRIVATE;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.mike.fragmentdemo3.R;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class EquipmentFragment extends Fragment implements View.OnClickListener {

    private ImageView mOnOffAirConditionsIv;
    private ImageView mOnOffSocketIv;
    private ImageView mOnOffAirPurifierIv;
    private ImageView mOnOffHumidifierIv;
    private ImageView mBgAirConditionerIv;
    private ImageView mBgSocketIv;
    private ImageView mBgAirPurifier;
    private ImageView mBgHumidifierIv;
    private ImageView mLoadingAirConditionerIv;
    private ImageView mLoadingSocketIv;
    private ImageView mLoadingAirPurifierIv;
    private ImageView mLoadingHumidifierTv;
    private ImageView mRefrigerationIv;
    private ImageView mHeatingIv;
    private ImageView mWarningAirConditionerIv;
    private ImageView mWarningSocketTv;
    private ImageView mWarningHumidifierIv;
    private ImageView mWarningAirPurifierIv;
    private TextView mWnOffAirConditioner;
    private TextView mStateSocketTv;
    private TextView mStatusAirPurifier;
    private TextView mStatusHumidifierTv;
    private ObjectAnimator mAnimator1;
    private ObjectAnimator mAnimator2;
    private ObjectAnimator mAnimator3;
    private ObjectAnimator mAnimator4;
    private Context context;

    private int mFlagAirConditioner = 1;
    private int mFlagSocket = 1;
    private int mFlagAirPurifier = 1;
    private int mFlagHumidifier = 1;
    private int control = 0;
    private int control2 = 1;

    private boolean mAcState;
    private boolean mAcState2;
    private boolean mSoState;
    private boolean mApState;
    private boolean mHuState;//0关闭，1开启
    private boolean mAcWaring = false;
    private boolean mSoWaring;
    private boolean mApWaring;
    private boolean mHuWaring;
    private boolean mRefrigeration = true;
    private boolean mHeating = true;
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    public EquipmentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content, container, false);
        return view;
    }

    private void saveSP() {
        FragmentActivity fragmentActivity = requireActivity();
        if (fragmentActivity != null) {
            SharedPreferences sp = fragmentActivity.getSharedPreferences("deviceInfo", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("acState", mAcState);
            editor.putBoolean("soState", mSoState);
            editor.putBoolean("apState", mApState);
            editor.putBoolean("huState", mHuState);//sw为真或假
            editor.putBoolean("acWaring", mAcWaring);
            editor.putBoolean("mSoWaring",mSoWaring);
            editor.putBoolean("mApWaring",mApWaring);
            editor.putBoolean("mHuWaring",mHuWaring);
            editor.commit();
        }

    }

    private void readSP() {
        SharedPreferences sp = requireActivity().getSharedPreferences("deviceInfo", MODE_PRIVATE);
        boolean acOn = sp.getBoolean("acState", false);
        boolean soOn = sp.getBoolean("soState", false);
        boolean apOn = sp.getBoolean("apState", false);
        boolean huOn = sp.getBoolean("huState", false);
        mAcWaring = sp.getBoolean("acWaring", false);
        mSoWaring = sp.getBoolean("mSoWaring",false);
        mApWaring = sp.getBoolean("mApWaring",false);
        mHuWaring = sp.getBoolean("mHuWaring",false);
        control = sp.getInt("temp", 0);

        if (control2 == 1 && control == 1) {
            //设置ImageView和TextView的状态
            mOnOffAirConditionsIv.setImageResource(acOn ? R.drawable.air_conditioner_on : R.drawable.air_conditioner_off);
            mWnOffAirConditioner.setText(acOn ? "On" : "Off");
            mBgAirConditionerIv.setImageResource(acOn ? R.drawable.shape_bg_background2 : R.drawable.shape_bg_background);
            mAcState = acOn;

            mOnOffSocketIv.setImageResource(soOn ? R.drawable.socket_on : R.drawable.socket_off);
            mStateSocketTv.setText(soOn ? "On" : "Off");
            mBgSocketIv.setImageResource(soOn ? R.drawable.shape_bg_background2 : R.drawable.shape_bg_background);
            mSoState = soOn;

            mOnOffAirPurifierIv.setImageResource(apOn ? R.drawable.air_purifier_on : R.drawable.air_purifier_off);
            mStatusAirPurifier.setText(apOn ? "On" : "Off");
            mBgAirPurifier.setImageResource(apOn ? R.drawable.shape_bg_background2 : R.drawable.shape_bg_background);
            mApState = apOn;

            mOnOffHumidifierIv.setImageResource(huOn ? R.drawable.humidifier_on : R.drawable.humidifier_off);
            mStatusHumidifierTv.setText(huOn ? "On" : "Off");
            mBgHumidifierIv.setImageResource(huOn ? R.drawable.shape_bg_background2 : R.drawable.shape_bg_background);
            mHuState = huOn;
        }
        if (control == 1 && mAcWaring == false) {
            mWarningAirConditionerIv.setBackgroundResource(R.drawable.safety);
            mBgAirConditionerIv.setEnabled(true);
        }
        if(control == 1 && mSoWaring == false){
            mWarningSocketTv.setBackgroundResource(R.drawable.safety);
            mBgSocketIv.setEnabled(true);
        }
        if(control == 1 && mApWaring == false){
            mWarningAirPurifierIv.setBackgroundResource(R.drawable.safety);
            mBgAirPurifier.setEnabled(true);
        }
        if(control == 1 && mHuWaring == false){
            mWarningHumidifierIv.setBackgroundResource(R.drawable.safety);
            mBgHumidifierIv.setEnabled(true);
        }


    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mOnOffAirConditionsIv = view.findViewById(R.id.Iv_on_off_air_conditions);
        mOnOffSocketIv = view.findViewById(R.id.Iv_on_off_socket);
        mOnOffAirPurifierIv = view.findViewById(R.id.Iv_on_off_air_purifier);
        mOnOffHumidifierIv = view.findViewById(R.id.Iv_on_off_hunidifier);
        mBgAirConditionerIv = view.findViewById(R.id.Iv_bg_air_conditioner);
        mBgSocketIv = view.findViewById(R.id.Iv_bg_Socket);
        mBgAirPurifier = view.findViewById(R.id.Iv_bg_air_purifier);
        mBgHumidifierIv = view.findViewById(R.id.Iv_bg_Humidifier);
        mWnOffAirConditioner = view.findViewById(R.id.Tv_on_off_air_conditioner);
        mStateSocketTv = view.findViewById(R.id.tv_state_socket);
        mStatusAirPurifier = view.findViewById(R.id.Tv_status_air_purifier);
        mStatusHumidifierTv = view.findViewById(R.id.tv_status_humidifier);
        mLoadingAirConditionerIv = view.findViewById(R.id.Iv_upload_air_conditioner);
        mLoadingSocketIv = view.findViewById(R.id.Iv_upload_socket);
        mLoadingAirPurifierIv = view.findViewById(R.id.Iv_upload_air_purifier);
        mLoadingHumidifierTv = view.findViewById(R.id.Iv_upload_humidifier);
        mRefrigerationIv = view.findViewById(R.id.refrigeration_Iv);
        mHeatingIv = view.findViewById(R.id.heating_Iv);
        mWarningAirConditionerIv = view.findViewById(R.id.warning_Air_conditioner_Iv);
        mWarningSocketTv = view.findViewById(R.id.warning_socket_tv);
        mWarningHumidifierIv = view.findViewById(R.id.warning_humidifier_Iv);
        mWarningAirPurifierIv = view.findViewById(R.id.warning_air_purifier_Iv);

        mBgAirConditionerIv.setOnClickListener(this);
        mBgSocketIv.setOnClickListener(this);
        mBgAirPurifier.setOnClickListener(this);
        mBgHumidifierIv.setOnClickListener(this);
        mRefrigerationIv.setOnClickListener(this);
        mHeatingIv.setOnClickListener(this);
        mWarningAirConditionerIv.setOnClickListener(this);
        mWarningSocketTv.setOnClickListener(this);
        mWarningHumidifierIv.setOnClickListener(this);
        mWarningAirPurifierIv.setOnClickListener(this);

        mAnimator1 = ObjectAnimator.ofFloat(mLoadingAirConditionerIv, "rotation", 0, 360.0F);
        mAnimator1.setDuration(3000);
        mAnimator1.setInterpolator(new LinearInterpolator());
        mAnimator1.setRepeatCount(-1);

        mAnimator2 = ObjectAnimator.ofFloat(mLoadingSocketIv, "rotation", 0, 360.0F);
        mAnimator2.setDuration(3000);
        mAnimator2.setInterpolator(new LinearInterpolator());
        mAnimator2.setRepeatCount(-1);

        mAnimator3 = ObjectAnimator.ofFloat(mLoadingAirPurifierIv, "rotation", 0, 360.0F);
        mAnimator3.setDuration(3000);
        mAnimator3.setInterpolator(new LinearInterpolator());
        mAnimator3.setRepeatCount(-1);

        mAnimator4 = ObjectAnimator.ofFloat(mLoadingHumidifierTv, "rotation", 0, 360.0F);
        mAnimator4.setDuration(3000);
        mAnimator4.setInterpolator(new LinearInterpolator());
        mAnimator4.setRepeatCount(-1);
        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
                readSP();
            }
        };
        SharedPreferences sp = getActivity().getSharedPreferences("deviceInfo", MODE_PRIVATE);
        sp.registerOnSharedPreferenceChangeListener(prefListener);
        readSP();
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        final Handler handler = new Handler();
        int id = v.getId();
        mFlagAirConditioner = 1;
        if (id == R.id.Iv_bg_air_conditioner) {

            if (mFlagAirConditioner == 1) {
                //检查当前状态并切换
                // 检查当前状态并切换
                if (mAcState) {
                    // 如果已经是开启状态，则立即或延迟关闭（这里我们立即关闭）
                    mOnOffAirConditionsIv.setImageResource(R.drawable.air_conditioner_off); // 或者使用turning_off图片并延迟
                    mWnOffAirConditioner.setText("Off");
                    mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_background);
                    mAcState = false;
                    saveSP();
                } else {
                    control2 = 0;
                    // 如果设备是关闭状态，则设置turning on的图片和文本，并延迟更改状态
                    mLoadingAirConditionerIv.setImageResource(R.drawable.loading1);
                    mAnimator1.start();
                    mFlagAirConditioner = 0;
                    mWnOffAirConditioner.setText("Turning On");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mWnOffAirConditioner.setText("Turning On");
                            mOnOffAirConditionsIv.setImageResource(R.drawable.air_conditioner_on);
                            mWnOffAirConditioner.setText("On");
                            mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_background2);
                            mLoadingAirConditionerIv.setImageResource(R.drawable.tab_menu_bg);
                            mAnimator1.cancel();
                            mAcState = true;
                            saveSP();
                            mFlagAirConditioner = 1;
                        }
                    }, 5000);
                }
                if (mAcState == false) {
                    mRefrigerationIv.setBackgroundResource(R.drawable.snow);
                    mHeatingIv.setBackgroundResource(R.drawable.sun1);
                    mHeating = true;
                    mRefrigeration = true;
                }
            }
        } else if (v.getId() == R.id.Iv_bg_Socket) {
            if (mFlagSocket == 1) {
                // 检查当前状态并切换
                if (mSoState) {
                    // 如果已经是开启状态，则立即或延迟关闭（这里我们立即关闭）
                    mOnOffSocketIv.setImageResource(R.drawable.socket_off); // 或者使用turning_off图片并延迟
                    mStateSocketTv.setText("Off");
                    mBgSocketIv.setImageResource(R.drawable.shape_bg_background);
                    mSoState = false;
                    saveSP();
                } else {
                    control2 = 0;
                    // 如果设备是关闭状态，则设置turning on的图片和文本，并延迟更改状态
                    mLoadingSocketIv.setImageResource(R.drawable.loading1);
                    mAnimator2.start();
                    mFlagSocket = 0;
                    mStateSocketTv.setText("Turning On");

                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mOnOffSocketIv.setImageResource(R.drawable.socket_on);
                            mStateSocketTv.setText("Turning On");
                            mStateSocketTv.setText("On");
                            mBgSocketIv.setImageResource(R.drawable.shape_bg_background2);
                            mLoadingSocketIv.setImageResource(R.drawable.tab_menu_bg);
                            mAnimator2.cancel();
                            mFlagSocket = 1;
                            mSoState = true;
                            saveSP();
                        }
                    }, 5000); // 延迟5秒
                }
            }
        } else if (v.getId() == R.id.Iv_bg_air_purifier) {
            if (mFlagAirPurifier == 1) {
                // 检查当前状态并切换
                if (mApState) {
                    // 如果已经是开启状态，则立即或延迟关闭（这里我们立即关闭）
                    mOnOffAirPurifierIv.setImageResource(R.drawable.air_purifier_off); // 或者使用turning_off图片并延迟
                    mStatusAirPurifier.setText("Off");
                    mBgAirPurifier.setImageResource(R.drawable.shape_bg_background);
                    mApState = false;
                    saveSP();
                } else {
                    control2 = 0;
                    // 如果设备是关闭状态，则设置turning on的图片和文本，并延迟更改状态
                    mLoadingAirPurifierIv.setImageResource(R.drawable.loading1);
                    mAnimator3.start();
                    mFlagAirPurifier = 0;
                    mStatusAirPurifier.setText("Turning On");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mOnOffAirPurifierIv.setImageResource(R.drawable.air_purifier_on);
                            mStatusAirPurifier.setText("Turning On");
                            mStatusAirPurifier.setText("On");
                            mBgAirPurifier.setImageResource(R.drawable.shape_bg_background2);
                            mLoadingAirPurifierIv.setImageResource(R.drawable.tab_menu_bg);
                            mFlagAirPurifier = 1;
                            mAnimator3.cancel();
                            mApState = true;
                            saveSP();
                        }
                    }, 5000); // 延迟5秒
                }
            }
        } else if (v.getId() == R.id.Iv_bg_Humidifier) {
            if (mFlagHumidifier == 1) {


                // 检查当前状态并切换
                if (mHuState) {
                    // 如果已经是开启状态，则立即或延迟关闭（这里我们立即关闭）
                    mOnOffHumidifierIv.setImageResource(R.drawable.humidifier_off); // 或者使用turning_off图片并延迟
                    mStatusHumidifierTv.setText("Off");
                    mBgHumidifierIv.setImageResource(R.drawable.shape_bg_background);
                    mHuState = false;
                    saveSP();
                } else {
                    control2 = 0;
                    // 如果设备是关闭状态，则设置turning on的图片和文本，并延迟更改状态
                    mLoadingHumidifierTv.setImageResource(R.drawable.loading1);
                    mAnimator4.start();
                    mFlagHumidifier = 0;
                    mStatusHumidifierTv.setText("Turning On");
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mOnOffHumidifierIv.setImageResource(R.drawable.humidifier_on);
                            mStatusHumidifierTv.setText("Turning On");
                            mStatusHumidifierTv.setText("On");
                            mBgHumidifierIv.setImageResource(R.drawable.shape_bg_background2);
                            mLoadingHumidifierTv.setImageResource(R.drawable.tab_menu_bg);
                            mAnimator4.cancel();
                            mHuState = true;
                            mFlagHumidifier = 1;
                            saveSP();
                        }
                    }, 5000); // 延迟5秒
                }
            }
        } else if (id == R.id.refrigeration_Iv) {
            if (mRefrigeration == true && mAcState == true) {
                mRefrigerationIv.setBackgroundResource(R.drawable.snow2);
                mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_backgroundsnow);
                mRefrigeration = false;
                mHeatingIv.setEnabled(false);

            } else if (mRefrigeration == false && mAcState == true) {
                mRefrigerationIv.setBackgroundResource(R.drawable.snow);
                mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_background2);
                mRefrigeration = true;
                mHeatingIv.setEnabled(true);
            }
        } else if (id == R.id.heating_Iv) {
            if (mHeating == true && mAcState == true) {
                mHeatingIv.setBackgroundResource(R.drawable.sun2);
                mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_backgroundsun);
                mRefrigerationIv.setEnabled(false);

                mHeating = false;

            } else if (mHeating == false && mAcState == true) {
                mRefrigerationIv.setEnabled(true);
                mHeatingIv.setBackgroundResource(R.drawable.sun1);
                mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_background2);
                mHeating = true;
            }
        } else if (v.getId() == R.id.warning_Air_conditioner_Iv) {
            if (mAcWaring) {
                mWarningAirConditionerIv.setBackgroundResource(R.drawable.safety);
                mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_background);
                mOnOffAirConditionsIv.setImageResource(R.drawable.air_conditioner_off);
                mAcState = false;
                mAcWaring = false;
                mBgAirConditionerIv.setEnabled(true);
                saveSP();
            } else {
                mWarningAirConditionerIv.setBackgroundResource(R.drawable.malfunction);
                mBgAirConditionerIv.setImageResource(R.drawable.shape_bg_background);
                mOnOffAirConditionsIv.setImageResource(R.drawable.air_conditioner_off);
                mBgAirConditionerIv.setEnabled(false);
                mAcState = false;
                mAcWaring = true;
                saveSP();
            }
        } else if (id == R.id.warning_socket_tv) {
            if (mSoWaring) {
                mBgSocketIv.setImageResource(R.drawable.shape_bg_background);
                mOnOffSocketIv.setImageResource(R.drawable.socket_off);
                mWarningSocketTv.setBackgroundResource(R.drawable.safety);
                mSoState = false;
                mSoWaring = false;
                mBgSocketIv.setEnabled(true);
                saveSP();
            } else {
                mBgSocketIv.setImageResource(R.drawable.shape_bg_background);
                mOnOffSocketIv.setImageResource(R.drawable.socket_off);
                mWarningSocketTv.setBackgroundResource(R.drawable.malfunction);
                mBgSocketIv.setEnabled(false);
                mSoState = false;
                mSoWaring = true;
                saveSP();
            }

        } else if (id == R.id.warning_air_purifier_Iv) {
            if (mApWaring) {
                mWarningAirPurifierIv.setBackgroundResource(R.drawable.safety);
                mBgAirPurifier.setImageResource(R.drawable.shape_bg_background);
                mOnOffAirPurifierIv.setImageResource(R.drawable.air_purifier_off);
                mApState = false;
                mApWaring = false;
                mBgAirPurifier.setEnabled(true);
                saveSP();
            } else {
                mWarningAirPurifierIv.setBackgroundResource(R.drawable.malfunction);
                mBgAirPurifier.setImageResource(R.drawable.shape_bg_background);
                mOnOffAirPurifierIv.setImageResource(R.drawable.air_purifier_off);
                mBgAirPurifier.setEnabled(false);
                mApState = false;
                mApWaring = true;
                saveSP();
            }
        } else if (id == R.id.warning_humidifier_Iv) {
            if (mHuWaring) {
                mWarningHumidifierIv.setBackgroundResource(R.drawable.safety);
                mOnOffHumidifierIv.setImageResource(R.drawable.humidifier_off);
                mBgHumidifierIv.setImageResource(R.drawable.shape_bg_background);
                mHuWaring = false;
                mHuState = false;
                mBgHumidifierIv.setEnabled(true);
                saveSP();
            } else {
                mWarningHumidifierIv.setBackgroundResource(R.drawable.malfunction);
                mOnOffHumidifierIv.setImageResource(R.drawable.humidifier_off);
                mBgHumidifierIv.setImageResource(R.drawable.shape_bg_background);
                mBgHumidifierIv.setEnabled(false);
                mHuWaring = true;
                mHuState = false;
                saveSP();
            }
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context; // 在这里初始化context
    }

    @Override
    public void onResume() {
        super.onResume();
        control2 = 1;
    }
}
